package cn.lkk.samples.lang.interfaces.clone;

import java.util.*;

/**
 * 演示如何使用对象的复制。
 * Object中定义了一个 protected 级别的 clone 方法，通过覆盖该方法使得该对象可以支持复制。
 */
public class Employee implements Cloneable {
    private String name;
    private double salary;
    private Date hireDay;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
        hireDay = new Date();
    }

    /**
     * 覆盖 Object 提供的 clone 方法，提供对象复制功能
     *
     * @return 复制的对象
     * @throws CloneNotSupportedException 如果对象没有实现 Cloneable 接口，那么将会抛出异常
     */
    @Override
    public Employee clone() throws CloneNotSupportedException {
        return (Employee) super.clone();
    }

    public void setHireDay(int year, int month, int day) {
        Calendar theHireDay = new GregorianCalendar(year, month - 1, day);
        this.hireDay = theHireDay.getTime();
    }

    public void raiseSalary(double byPercent) {
        double raise = salary * byPercent / 100;
        salary += raise;
    }

    @Override
    public String toString() {
        return "Employee[name=" + name
                + ", salary=" + salary
                + ", hireDay=" + hireDay
                + "]";
    }
}
